package com.example.shellcollector.service;

import android.app.Service;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStatsManager;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.shellcollector.Entity.InstalledAppUsage;
import com.example.shellcollector.database.InstalledAppUsageDao;
import com.example.shellcollector.database.ShellCollectorDatabase;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class AppInstalledCollectService extends Service {
    private Timer timer;
    private TimerTask task;
    private InstalledAppUsageDao installedAppUsageDao;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                List<InstalledAppUsage> installedAppUsageList = getInstalledAppsFromSystem();
                insertInstalledAppsIntoDB(installedAppUsageList);
            }
        };

        installedAppUsageDao = ShellCollectorDatabase.getDatabase(this).getInstalledAppUsageDao();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "start collecting installed apps", Toast.LENGTH_SHORT).show();

        timer.scheduleAtFixedRate(task, 0, 1000);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "stop collecting installed apps", Toast.LENGTH_SHORT).show();
        task.cancel();
        timer.cancel();
    }

    // --------------------------
    // utility methods

    private List<InstalledAppUsage> getInstalledAppsFromSystem() {
        PackageManager pm = getPackageManager();
        List<InstalledAppUsage> installedAppUsageList = new ArrayList<>();
        List<ApplicationInfo> appsInstalled = pm.getInstalledApplications(0);
        for (ApplicationInfo appInfo : appsInstalled) {
            String appName = appInfo.loadLabel(pm).toString();
            String packages = appInfo.packageName;
            installedAppUsageList.add(new InstalledAppUsage(appName, packages));
        }

        return installedAppUsageList;
    }

    private void insertInstalledAppsIntoDB(List<InstalledAppUsage> installedAppUsageList) {
        installedAppUsageList.removeIf(appUsage -> installedAppUsageDao.isAppInstalled(appUsage.getAppPackageName()));

        // set some attributes for newly installed apps
        for (InstalledAppUsage newlyInstalledAppUsage : installedAppUsageList) {
            newlyInstalledAppUsage.setFirstRunningTime(-1);
            newlyInstalledAppUsage.setLastRunningTime(-1);
            newlyInstalledAppUsage.setTotalRunningTime(0);
            newlyInstalledAppUsage.setLaunchCount(0);
        }

        installedAppUsageDao.insertAll(installedAppUsageList);
    }
}
